package com.admin.business.voteOrgInfo;

import com.admin.business.voteEssentialInformation.VoteEssentialInformationDao;
import com.alibaba.druid.util.StringUtils;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 选聘物业公司
 * @create: 2023-06-14 14:40:46
 **/
@Slf4j
@Service("voteOrgInfoService")

public class voteOrgInfoService {

    @Resource
    VoteOrgInfoDao voteOrgInfoDao;

    @Resource
    VoteEssentialInformationDao voteEssentialInformationDao;

    /**
     * 添加选聘物业公司,获取id数据
     *
     * @return
     * @author zhh
     * @create: 2023-06-14 14:40:46
     */
    @OpenApi
    public Return addVoteOrgInfo(CDO cdoRequest) {
        voteOrgInfoDao.addVoteOrgInfo(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新选聘物业公司数据
     *
     * @return
     * @author zhh
     * @create: 2023-06-14 14:40:46
     */
    @OpenApi
    public Return updateVoteOrgInfoById(CDO cdoRequest) {
        voteOrgInfoDao.updateVoteOrgInfoById(cdoRequest);
        return Return.SUCCESS;
    }

    /**
     * 根据id，删除选聘物业公司数据
     *
     * @param cdoRequest - id:
     * @return
     * @author zhh
     * @create: 2023-06-14 14:40:46
     */
    @OpenApi
    public Return deleteVoteOrgInfoById(CDO cdoRequest) {
        int count = voteOrgInfoDao.deleteVoteOrgInfoById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("count", count);
        return Return.valueOf(cdoResult);
    }


    /**
     * 根据ID，获取选聘物业公司数据
     *
     * @param cdoRequest - id:
     * @return
     * @author zhh
     * @create: 2023-06-14 14:40:46
     */
    @OpenApi
    public Return getVoteOrgInfoById(CDO cdoRequest) {
        CDO cdoMapperResult = voteOrgInfoDao.getVoteOrgInfoById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 选聘物业都未通过，编辑state为已删除
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return delVoteNotPassOrgInfo(CDO cdoRequest){
        voteOrgInfoDao.delOrgInfoByPid(cdoRequest);
        return Return.SUCCESS;
    }


    /**
     * 修改正在投票的物业公司文件信息
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return updateVoteOrgFileInfo(CDO cdoRequest){
        List<CDO> orgInfoList = voteOrgInfoDao.getVoteOrgInfoListPage(cdoRequest);
        if(orgInfoList == null || orgInfoList.size() == 0){
            return Return.valueOf(-2,"投票物业公司信息异常，请联系客服");
        }
        CDO update = new CDO();
        update.set("id",orgInfoList.get(0).getString("id"));
        update.set("org_introduce_file",cdoRequest.getString("org_introduce_file"));
        update.set("org_introduce_file_name",cdoRequest.getString("org_introduce_file_name"));
        update.set("org_other_file",cdoRequest.getString("org_other_file"));
        update.set("org_other_file_name",cdoRequest.getString("org_other_file_name"));
        voteOrgInfoDao.updateVoteOrgInfoById(update);
        return Return.SUCCESS;
    }


    /**
     * 获取已经投票的物业公司数量
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return getAlreadyVoteOrgTotal(CDO cdoRequest){
        int alreadyVoteOrgTotal = voteOrgInfoDao.getAlreadyVoteOrgTotal(cdoRequest);
        return Return.valueOf(alreadyVoteOrgTotal);
    }



    /**
     * 根据非空条件，获取选聘物业公司数据列表
     *
     * @param cdoRequest
     * @return
     * @author zhh
     * @create: 2023-06-14 14:40:46
     */
    @OpenApi
    public Return getVoteOrgInfoListPage(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.getInteger("pageIndex") - 1;
        int recordSize = 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> voteOrgList = voteOrgInfoDao.getVoteOrgInfoListPage(cdoRequest);
        for (CDO voteOrgInfo : voteOrgList) {
            CDO selectPosition = new CDO();
            selectPosition.set("community_province", voteOrgInfo.getString("org_province"));
            selectPosition.set("community_city", voteOrgInfo.getString("org_city"));
            selectPosition.set("community_area", voteOrgInfo.getString("org_district"));
            List<CDO> addressList = voteEssentialInformationDao.getVoteAddressInformation(selectPosition);
            String address = "";
            for (CDO addressInfo : addressList) {
                if (StringUtils.isEmpty(address)) {
                    address = addressInfo.getString("name");
                } else {
                    address = address + "/" + addressInfo.getString("name");
                }
            }
            voteOrgInfo.set("address",address);
        }
        boolean isEndPage = false;
        if (voteOrgList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", voteOrgList);
        int total = voteOrgInfoDao.getVoteOrgInfoCount(cdoRequest);
        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
